var oTable;
$.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource, fnCallback, bStandingRedraw )
{
    if ( typeof sNewSource != 'undefined' && sNewSource != null ) {
        oSettings.sAjaxSource = sNewSource;
    }
    
    // Server-side processing should just call fnDraw
    if ( oSettings.oFeatures.bServerSide ) {
        this.fnDraw();
        return;
    }
    
    this.oApi._fnProcessingDisplay( oSettings, true );
    var that = this;
    var iStart = oSettings._iDisplayStart;
    var aData = [];
    
    this.oApi._fnServerParams( oSettings, aData );
    
    oSettings.fnServerData.call( oSettings.oInstance, oSettings.sAjaxSource, aData, function(json) {
        /* Clear the old information from the table */
        that.oApi._fnClearTable( oSettings );
        
        /* Got the data - add it to the table */
        var aData =  (oSettings.sAjaxDataProp !== "") ?
        that.oApi._fnGetObjectDataFn( oSettings.sAjaxDataProp )( json ) : json;
        
        for ( var i=0 ; i<aData.length ; i++ )
        {
            that.oApi._fnAddData( oSettings, aData[i] );
        }
        
        oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
        
        if ( typeof bStandingRedraw != 'undefined' && bStandingRedraw === true )
        {
            oSettings._iDisplayStart = iStart;
            that.fnDraw( false );
        }
        else
        {
            that.fnDraw();
        }
        
        that.oApi._fnProcessingDisplay( oSettings, false );
        
        /* Callback user function - for event handlers etc */
        if ( typeof fnCallback == 'function' && fnCallback != null )
        {
            fnCallback( oSettings );
        }
    }, oSettings );
};

function reloadTable() {
    if (oTable) {
        oTable.fnDestroy();
    }
    oTable = $('#aacademica_table').dataTable( {                                                
        "oLanguage": {
            "sLengthMenu": "_MENU_ records per page"
        },
        "sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
        "sPaginationType": "bootstrap",
        aoColumnsDef: [],    
        bLengthChange: false,
        "aoColumns": [
        {
            "sTitle": ""
        },
        {
            "sTitle": "Nombre"
        },
        {
            "sTitle": "Descripci&oacute;n"
        }
        ],
        "sAjaxSource": "/admin/aacademica/list",    
        "fnServerData": function ( sSource, aoData, fnCallback ) {
            $.ajax({
                "url": sSource,
                "data": aoData,
                "success": fnCallback,
                "contentType": "application/json; charset=utf-8",
                "dataType": "json",
                "cache": false,
                "error": function (xhr, error, thrown) {                        
                    
                }
            });    
        }                
    });                         

}

function nuevo() {
    window.location.href = '/admin/aacademica/edit/';
}

function deleteArea(codigo) {
    bootbox.confirm("Desea eliminar el Area Academica?", function(result) {
        if(result) {
            $.ajax({
                url:'/admin/aacademica/delete/id/' + codigo,                
                type: 'post',
                dataType: 'json',                
                success: function(data) {
                    if(data.response == "error") {                        
                        bootbox.alert("Error! " + data.message, function() {});
                    } else {
                        bootbox.alert("Eliminado correctamente", function() {});
                    }
                },
                error: function() {                    
                    bootbox.alert("Ocurrio un error inesperado!", function() {});
                }
            });
            reloadTable();
        }                         
    });
}

$(document).ready(function() {
    menuClass("area-academica");
    $.blockUI({
        message: "Aguarde un momento por favor"
    });
    $('#btnnuevo').attr('onclick','nuevo()');     
    reloadTable();
    $.unblockUI(); 
});